package com.ls.markt.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ls.markt.core.utils.AgeUtil;
import com.ls.markt.core.utils.DateUtils;
import com.ls.markt.model.admin.Player;
import com.ls.markt.service.admin.ClubService;
import com.ls.markt.service.admin.PlayerService;
import com.ls.markt.util.WebResult;
import com.ls.markt.vo.Response;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;

/**
 * @author ls
 * 球员 控制器类
 */
@Controller
@RequestMapping("/player")
public class PlayerController {

    @Resource
    private PlayerService playerService;

    @Resource
    private ClubService clubService;

    @GetMapping("/index")
    public String index(){
        return "admin/player/index";
    }

    /**
     * 分页查询所有球员记录
     * @param page
     * @param rows
     * @return
     * @throws ParseException
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(@RequestParam(value="page",required=false,defaultValue="1")int page,
                          @RequestParam(value="rows",required=false,defaultValue="10")int rows) throws ParseException {
        Sort sort = Sort.by(Sort.Direction.DESC, "marketValue");
        PageRequest pageRequest = PageRequest.of(page-1,rows,sort);
        Page<Player> players = playerService.findAll(pageRequest);

        JSONArray jsonArray = new JSONArray();
        if (players.getSize()>0){
            for (Player player:players){
                JSONObject json = (JSONObject)JSONObject.toJSON(player);
                json.put("clubName",clubService.getClubName(player.getClub()));
                json.put("age", DateUtils.format(player.getBirthday(),"yyyy-MM-dd")+"("+ AgeUtil.getAgeByBirth(player.getBirthday())+")");
                jsonArray.add(json);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",players.getTotalElements());
        jsonObject.put("rows",jsonArray);
        return jsonObject;
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @GetMapping("/add")
    public String add(){
        return "admin/player/add";
    }

    /**
     * 跳转到编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id")Long id, Model model){
        Player player = playerService.getPlayerById(id);
        model.addAttribute("player",player);
        return new  ModelAndView("admin/player/edit","playerModel",model);
    }

    /**
     * 保存，更新数据
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Response save(Player player){
        playerService.savePlayer(player);
        return new Response(true);
    }

    /**
     * 执行删除球员
     * @param ids
     * @return
     */
    @PostMapping("/doDelete")
    @ResponseBody
    public Response doDelete(@RequestParam Long[] ids){
        playerService.removePlayer(ids);
        return new Response(true);
    }

    @PostMapping("/import")
    @ResponseBody
    public JSONObject importExcel(@RequestParam(name = "file") MultipartFile file) throws IOException {
        playerService.importExcel(file);
        return WebResult.success();
    }

}
